<template>
	<div class="node-wrap">
		<div class="node-wrap-box" @click="show">
			<div class="title" style="background: #3296fa;">
				<el-icon><el-icon-cpu /></el-icon>
				<span>{{ nodeConfig.name }}</span>
			</div>
			<div class="content">
				<span v-if="toText(nodeConfig)">{{ toText(nodeConfig) }}</span>
				<span v-else class="placeholder">请设置</span>
			</div>
		</div>
		<add-node v-model="nodeConfig.childNode"></add-node>
		<el-drawer v-model="drawer" :title="nodeConfig.name" destroy-on-close append-to-body>
			<el-form :model="form" label-width="120px">
				<el-form-item label="名称">
					<el-input v-model="form.name"></el-input>
				</el-form-item>
				<el-form-item label="命令">
					<el-input v-model="form.command"></el-input>
				</el-form-item>
				<el-form-item label="参数">
					<el-input v-model="form.args" type="textarea"></el-input>
				</el-form-item>
				<el-form-item label="超时">
					<el-input v-model="form.timeout"></el-input>
				</el-form-item>
				<el-form-item label="后端">
					<el-select v-model="form.backend">
						<el-option label="bash" value="bash"></el-option>
						<el-option label="docker" value="docker"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="镜像" v-if="form.backend === 'docker'">
					<el-input v-model="form.image"></el-input>
				</el-form-item>
				<el-form-item label="环境变量">
					<el-input v-model="form.env_map" type="textarea"></el-input>
				</el-form-item>
				<el-form-item label="重试次数">
					<el-input-number v-model="form.retries" :min="0"></el-input-number>
				</el-form-item>
				<el-form-item label="依赖">
					<el-input v-model="form.depends" type="textarea"></el-input>
				</el-form-item>
				<el-form-item label="断言">
					<el-input v-model="form.raw_asserts" type="textarea"></el-input>
				</el-form-item>
			</el-form>
			<template #footer>
				<el-button @click="drawer = false">取消</el-button>
				<el-button type="primary" @click="save">保存</el-button>
			</template>
		</el-drawer>
	</div>
</template>

<script>
	import addNode from '@/components/scWorkflow/nodes/addNode.vue'

	export default {
		props: {
			modelValue: { type: Object, default: () => {} }
		},
		components: {
			addNode
		},
		data() {
			return {
				nodeConfig: {},
				drawer: false,
				form: {}
			}
		},
		watch:{
			modelValue(val){
				this.nodeConfig = val
			}
		},
		mounted() {
			this.nodeConfig = this.modelValue
		},
		methods: {
			show(){
				this.form = Object.assign({}, this.nodeConfig)
				this.drawer = true
			},
			save(){
				Object.assign(this.nodeConfig, this.form)
				this.drawer = false
				this.$emit("update:modelValue", this.nodeConfig)
			},
			toText(nodeConfig){
				return nodeConfig.command
			}
		}
	}
</script>

<style>
</style>
